package cn.kgc.controller;

import cn.kgc.bean.dto.ProcessDto;
import cn.kgc.bean.pojo.CCase;
import cn.kgc.bean.pojo.CProcess;
import cn.kgc.common.Constants;
import cn.kgc.common.Result;
import cn.kgc.service.CaseService;
import cn.kgc.service.ProcessService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/process")
public class ProcessController {
    @Resource
    private ProcessService processService;
    @Resource
    private CaseService caseService;

    @GetMapping("/getProcessInfo")
    public Result getProcessInfo(@RequestParam(defaultValue = "0") long status,
                              @RequestParam String courtId,
                              @RequestParam String username,
                              @RequestParam(required = false) int pageNum,
                              @RequestParam(required = false) int pageSize){
        QueryWrapper<CProcess> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",status);
        queryWrapper.eq("courtId",courtId);
        queryWrapper.eq("username",username);
        Page page = processService.page(new Page<>(pageNum,pageSize),queryWrapper);
        if (page != null){
            return Result.success(Constants.CODE_200,"",page);
        }else {
            return Result.error();
        }
    }


    @GetMapping("/getDealting")
    public Result getDealting(@RequestParam(defaultValue = "1") long status,
                                 @RequestParam String courtId,
                                 @RequestParam String username,
                                 @RequestParam(required = false) int pageNum,
                                 @RequestParam(required = false) int pageSize){
        QueryWrapper<CProcess> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",status);
        queryWrapper.eq("courtId",courtId);
        queryWrapper.eq("username",username);
        Page page = processService.page(new Page<>(pageNum,pageSize),queryWrapper);
        if (page != null){
            return Result.success(Constants.CODE_200,"",page);
        }else {
            return Result.error();
        }
    }

    @GetMapping("/getDealted")
    public Result getDealted(@RequestParam(defaultValue = "2") long status,
                              @RequestParam String courtId,
                              @RequestParam String undertaker,
                              @RequestParam(required = false) int pageNum,
                              @RequestParam(required = false) int pageSize){
        QueryWrapper<CCase> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",status);
        queryWrapper.eq("court",courtId);
        queryWrapper.eq("undertaker",undertaker);
        Page page = caseService.page(new Page<>(pageNum,pageSize),queryWrapper);
        if (page != null){
            return Result.success(Constants.CODE_200,"",page);
        }else {
            return Result.error();
        }
    }


    @GetMapping("/getWorkManager")
    public Result getWorkManager(
                             @RequestParam(required = false) int pageNum,
                             @RequestParam(required = false) int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<ProcessDto> list = processService.getProcessAllInfo();
        PageInfo<ProcessDto> page = new PageInfo<>(list);
        if (page != null){
            return Result.success(Constants.CODE_200,"",page);
        }else {
            return Result.error();
        }
    }
    @GetMapping("/addProcessInfo")
    public Result addProcessInfo(@RequestBody CProcess cProcess){
       boolean count = processService.save(cProcess);
        if (count){
            return Result.success();
        }else {
            return Result.error();
        }
    }

}
